web/src/pages/search/[...searchTerm].astro

192 lines · 4735 bytes

  1---
  2  import Fuse from 'fuse.js';
  3
  4import Layout from '@layouts/Layout.astro';
  5import { fetchData, slugify } from '@utils/fetch-data';
  6import { prepareSearchItems, searchOptions } from '@utils/do-searchy-searchy';
  7import Search from '@components/things/Search.svelte';
  8import SmartSuggestions from '@components/things/SmartSuggestions.svelte';
  9import FontAwesome from '@components/form/FontAwesome.svelte';
 10
 11import type { Service } from '../../types/Service';
 12
 13export const prerender = false;
 14
 15let fuse: Fuse<any>;
 16
 17const categories = (await fetchData())?.categories;
 18
 19const items = prepareSearchItems(categories);
 20
 21fuse = new Fuse(items, searchOptions);
 22
 23const searchTerm = Astro.params.searchTerm;
 24
 25const searchResults = fuse.search(searchTerm || '').map(result => result.item);
 26
 27const services = searchResults.filter(result => result.type === 'Service');
 28
 29const putResultsIntoGroups = () => {
 30  const grouped = services.reduce((acc, item) => {
 31    const { category: categoryName, sectionName, ...service } = item;
 32
 33    if (!acc[categoryName]) {
 34      acc[categoryName] = { categoryName, sections: {} };
 35    }
 36
 37    if (!acc[categoryName].sections[sectionName]) {
 38      acc[categoryName].sections[sectionName] = { sectionName, items: [] };
 39    }
 40
 41    acc[categoryName].sections[sectionName].items.push(service);
 42
 43    return acc;
 44  }, {});
 45
 46  // Convert the grouped object into the desired array structure.
 47  // And fuck it, let's use `any`
 48  return Object.values(grouped).map((category: any) => ({
 49    categoryName: category.categoryName,
 50    sections: Object.values(category.sections)
 51  }));
 52};
 53
 54const beer = putResultsIntoGroups();
 55
 56---
 57
 58<Layout title="Search | Awesome Privacy">
 59  <section>
 60    <h1>Search</h1>
 61    <Search client:visible data={categories} previousSearch={searchTerm} />
 62  </section>
 63  <SmartSuggestions client:visible searchTerm={searchTerm || ''} categories={categories} />
 64  <section class="result-count">
 65    <h3>Deep Search</h3>
 66    <p>Showing {services.length} results for "{searchTerm}" sorted by relevence</p>
 67  </section>
 68  <div class="results">
 69    {
 70      beer.map((category: any) => (
 71        <div class="category">
 72          <a class="category-title" href={`/${slugify(category.categoryName)}`}>
 73            <h3>{category.categoryName}</h3>
 74          </a>
 75          <span class="section-icon"><FontAwesome iconName={slugify(category.categoryName)} /></span>
 76          <ul class="section-list">
 77            {category.sections.map((section: any) => (
 78              <li class="section-item">
 79                <h4>{section.sectionName}</h4>
 80                <ul class="service-list">
 81                  {section.items.map((item: Service) => (
 82                    <li>
 83                      <a href={item.url}>{item.name}</a>
 84                    </li>
 85                  ))}
 86                </ul>
 87              </li>
 88            ))}
 89          </ul>
 90        </div>
 91      ))
 92    }
 93</div>
 94</Layout>
 95
 96
 97<style lang="scss">
 98
 99section {
100  display: flex;
101  flex-direction: column;
102  padding: 1rem;
103  margin: 2rem auto;
104  padding: 1rem;
105  width: calc(100% - 2rem);
106  max-width: 1100px;
107
108  h1 {
109    font-size: 3rem;
110		color: var(--accent-3);
111		font-family: "Lekton", sans-serif;
112		text-align: center;
113		margin: 1rem 0;
114  }
115}
116
117.result-count {
118  padding: 0;
119  width: 80vw;
120  max-width: 900px;
121  margin: 1rem auto 0 auto;
122  h3 {
123    margin: 0;
124    color: var(--accent-3);
125    font-size: 1.6rem;
126  }
127  p {
128    margin: 0;
129    opacity: 0.6;
130  }
131}
132
133.results {
134  margin: 0 auto 2rem auto;
135  padding: 1rem;
136  width: calc(100% - 2rem);
137  max-width: 1100px;
138  columns: 6 300px;
139  column-gap: 1rem;
140  .category {
141    padding: 1rem;
142    box-shadow: 4px 4px 0 var(--foreground);
143    background: var(--accent-fg);
144    border: 2px solid var(--foreground);
145    border-radius: var(--curve-sm);
146    display: inline-flex;
147    flex-direction: column;
148    width: 85%;
149    margin: 1rem;
150    position: relative;
151    .category-title {
152      text-decoration: none;
153      color: var(--foreground);
154      z-index: 2;
155      position: relative;
156      h3 {
157        font-family: "Lekton", sans-serif;
158        font-weight: bold;
159        margin: 0;
160        font-size: 1.8rem;
161      }
162    }
163    .section-icon {
164      position: absolute;
165      right: 0.5rem;
166      top: 0.5rem;
167      width: fit-content;
168      :global(svg) {
169        width: 2rem;
170        height: 2rem;
171        opacity: 0.5;
172        text-shadow: 3px 3px 0 black;
173        color: var(--accent-3);
174        transition: all 0.2s ease-in-out;
175      }
176    }
177    .section-list {
178      list-style: none;
179      padding: 0;
180      margin: 0;
181      h4 {
182        margin: 1rem 0 0 0;
183      }
184      .service-list {
185        list-style: none;
186        padding: 0;
187        margin: 0;
188      }
189    }
190  }
191}
192</style>